import ffmpeg
import json

def get_video_info(file):
    try:
        probe = ffmpeg.probe(file)
        video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
        audio_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'audio'), None)
        '''
        video_stream 是 dict, json.dumps() 可以将其转为字符串
        '''
        print("视频信息：", json.dumps(video_stream,indent=4))
        print("音频信息：", json.dumps(audio_stream,indent=4))
    except Exception as e:
        print("获取视频信息失败：", e)


if __name__ == "__main__":
    input_file = "../video/112.mkv"
    get_video_info(input_file)

# 运行结果:
video_stream = {
    "index": 0,
    "codec_name": "h264",
    "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
    "profile": "High",
    "codec_type": "video",
    "codec_tag_string": "[0][0][0][0]",
    "codec_tag": "0x0000",
    "width": 1920,
    "height": 808,
    "coded_width": 1920,
    "coded_height": 808,
    "closed_captions": 0,
    "has_b_frames": 2,
    "sample_aspect_ratio": "1:1",
    "display_aspect_ratio": "240:101",
    "pix_fmt": "yuv420p",
    "level": 40,
    "color_range": "tv",
    "color_space": "bt709",
    "color_transfer": "bt709",
    "color_primaries": "bt709",
    "chroma_location": "left",
    "field_order": "progressive",
    "refs": 1,
    "is_avc": "true",
    "nal_length_size": "4",
    "r_frame_rate": "24/1",
    "avg_frame_rate": "24/1",
    "time_base": "1/1000",
    "start_pts": 0,
    "start_time": "0.000000",
    "bits_per_raw_sample": "8",
    "disposition": {
        "default": 1,
        "dub": 0,
        "original": 0,
        "comment": 0,
        "lyrics": 0,
        "karaoke": 0,
        "forced": 0,
        "hearing_impaired": 0,
        "visual_impaired": 0,
        "clean_effects": 0,
        "attached_pic": 0,
        "timed_thumbnails": 0
    },
    "tags": {
        "BPS": "1808201",
        "DURATION": "01:25:25.709000000",
        "NUMBER_OF_FRAMES": "123017",
        "NUMBER_OF_BYTES": "1158539123",
        "_STATISTICS_WRITING_APP": "mkvmerge v86.0 ('Winter') 64-bit",
        "_STATISTICS_WRITING_DATE_UTC": "2024-08-31 01:33:26",
        "_STATISTICS_TAGS": "BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES"
    }
}
audio_stream = {
    "index": 1,
    "codec_name": "eac3",
    "codec_long_name": "ATSC A/52B (AC-3, E-AC-3)",
    "codec_type": "audio",
    "codec_tag_string": "[0][0][0][0]",
    "codec_tag": "0x0000",
    "sample_fmt": "fltp",
    "sample_rate": "48000",
    "channels": 2,
    "channel_layout": "stereo",
    "bits_per_sample": 0,
    "dmix_mode": "-1",
    "ltrt_cmixlev": "-1.000000",
    "ltrt_surmixlev": "-1.000000",
    "loro_cmixlev": "-1.000000",
    "loro_surmixlev": "-1.000000",
    "r_frame_rate": "0/0",
    "avg_frame_rate": "0/0",
    "time_base": "1/1000",
    "start_pts": 0,
    "start_time": "0.000000",
    "bit_rate": "96000",
    "disposition": {
        "default": 1,
        "dub": 0,
        "original": 0,
        "comment": 0,
        "lyrics": 0,
        "karaoke": 0,
        "forced": 0,
        "hearing_impaired": 0,
        "visual_impaired": 0,
        "clean_effects": 0,
        "attached_pic": 0,
        "timed_thumbnails": 0
    },
    "tags": {
        "language": "chi",
        "title": "Mandarin",
        "BPS": "96000",
        "DURATION": "01:25:25.760000000",
        "NUMBER_OF_FRAMES": "160180",
        "NUMBER_OF_BYTES": "61509120",
        "_STATISTICS_WRITING_APP": "mkvmerge v86.0 ('Winter') 64-bit",
        "_STATISTICS_WRITING_DATE_UTC": "2024-08-31 01:33:26",
        "_STATISTICS_TAGS": "BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES"
    }
}